package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.CategoryDetailsData;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 晒单 分类 下二级页面下方信息的适配器
 * Created by lanouhn on 16/9/6.
 */
public class CategoryDetailsAdapter extends RecyclerView.Adapter<CategoryDetailsAdapter.ViewHolder> {
    private Context context;
    private List<CategoryDetailsData.DataBean> categoryDetailsData;
    private OnRVItemClickListener clickListener;
    private OnRVItemLongClickListener longClickListener;

    public void setClickListener(OnRVItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(OnRVItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public CategoryDetailsAdapter(Context context, List<CategoryDetailsData.DataBean> categoryDetailsData) {
        this.context = context;
        this.categoryDetailsData = categoryDetailsData;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mygridview_categoryclass, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Picasso.with(context).load(categoryDetailsData.get(position).getMini_pic_url()).config(Bitmap.Config.RGB_565).into(holder.iv_item_mygridview_categoryclass);
        Picasso.with(context).load(categoryDetailsData.get(position).getAuthor().getAvatar()).config(Bitmap.Config.RGB_565).into(holder.iv_item_header_mygridview_categoryclass);
        holder.tv_item_author_mygridview_categoryclass.setText(categoryDetailsData.get(position).getAuthor().getNickname());
        holder.tv_item_likes_mygridview_categoryclass.setText(categoryDetailsData.get(position).getDynamic().getLikes());
        holder.tv_item_content_mygridview_categoryclass.setText(categoryDetailsData.get(position).getContent());
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    clickListener.OnRVItemClickListener(clickposition);
                }
            });
        }
        if (longClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    longClickListener.OnRVItemLongClickListener(clickposition);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return categoryDetailsData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_mygridview_categoryclass;// 大图
        private CircleImageView iv_item_header_mygridview_categoryclass;// 作者头像
        private TextView tv_item_author_mygridview_categoryclass;// 作者名
        private TextView tv_item_likes_mygridview_categoryclass;// 喜欢数量
        private TextView tv_item_content_mygridview_categoryclass;// 正文

        public ViewHolder(View itemView) {
            super(itemView);
            iv_item_mygridview_categoryclass = (ImageView) itemView.findViewById(R.id.iv_item_mygridview_categoryclass);// 大图
            iv_item_header_mygridview_categoryclass = (CircleImageView) itemView.findViewById(R.id.iv_item_header_mygridview_categoryclass);// 作者头像
            tv_item_author_mygridview_categoryclass = (TextView) itemView.findViewById(R.id.tv_item_author_mygridview_categoryclass);// 作者名
            tv_item_likes_mygridview_categoryclass = (TextView) itemView.findViewById(R.id.tv_item_likes_mygridview_categoryclass);// 喜欢数量
            tv_item_content_mygridview_categoryclass = (TextView) itemView.findViewById(R.id.tv_item_content_mygridview_categoryclass);// 正文
        }
    }
}
